package com.mac.intellect_flow.flowable.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.intellect_flow.flowable.service.SysFormService;
import com.mac.intellect_flow.reply.PageParam;
import com.mac.intellect_flow.reply.PageResult;
import com.mac.intellect_flow.sys.entity.SysExpression;
import com.mac.intellect_flow.sys.entity.SysForm;
import com.mac.intellect_flow.sys.mapper.SysFormMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 流程表单Service业务层处理
 * 
 * @author mac
 */
@Service
public class SysFormServiceImpl implements SysFormService {
    @Autowired
    private SysFormMapper sysFormMapper;

    /**
     * 查询流程表单
     * 
     * @param formId 流程表单ID
     * @return 流程表单
     */
    @Override
    public SysForm selectSysFormById(Long formId)
    {
        return sysFormMapper.selectSysFormById(formId);
    }

    /**
     * 查询流程表单列表
     * 
     * @param sysForm 流程表单
     * @return 流程表单
     */
    @Override
    public List<SysForm> selectSysFormList(SysForm sysForm)
    {
        return sysFormMapper.selectSysFormList(sysForm);
    }

    /**
     * 新增流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int insertSysForm(SysForm sysForm) {
        sysForm.setCreateTime(LocalDateTime.now());
        return sysFormMapper.insertSysForm(sysForm);
    }

    /**
     * 修改流程表单
     * 
     * @param sysForm 流程表单
     * @return 结果
     */
    @Override
    public int updateSysForm(SysForm sysForm) {
        sysForm.setUpdateTime(LocalDateTime.now());
        return sysFormMapper.updateSysForm(sysForm);
    }

    /**
     * 批量删除流程表单
     * 
     * @param formIds 需要删除的流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormByIds(Long[] formIds)
    {
        return sysFormMapper.deleteSysFormByIds(formIds);
    }

    /**
     * 删除流程表单信息
     * 
     * @param formId 流程表单ID
     * @return 结果
     */
    @Override
    public int deleteSysFormById(Long formId)
    {
        return sysFormMapper.deleteSysFormById(formId);
    }

    @Override
    public PageResult<SysForm> queryPage(SysForm sysForm, PageParam pageParam) {
        Page<SysForm> page = new Page<>(pageParam.getPageNo(), pageParam.getPageSize());
        LambdaQueryWrapper<SysForm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(sysForm.getFormName()),SysForm::getFormName, sysForm.getFormName())
                .eq(StringUtils.isNotEmpty(sysForm.getFormContent()), SysForm::getFormContent, sysForm.getFormContent());
        Page<SysForm> sysFormPage = sysFormMapper.selectPage(page, queryWrapper);
        return new PageResult<>(sysFormPage.getRecords(),sysFormPage.getTotal(),pageParam.getPageSize(), pageParam.getPageNo());
    }
}
